package site.zhanjingbo.shop.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import site.zhanjingbo.shop.dao.ProductDao;
import site.zhanjingbo.shop.meta.Product;
import site.zhanjingbo.shop.meta.User;
import site.zhanjingbo.shop.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

	@Resource
	private ProductDao productDao;

	public Product getProductById(int id) {
		return productDao.getProductById(id);
	}

	public List<Product> getProducts() {
		return productDao.getProducts();
	}

	public List<Product> getProductsByUser(User user) {
		return productDao.getProductsByUser(user);
	}

	public boolean addProduct(Product product) {
		boolean flag = false;
		// 标题长度[2,80]
		if (product.getTitle().length() < 2 || product.getTitle().length() > 80) {
			return flag;
		}
		// 概要长度[2,140]
		if (product.getSummary().length() < 2 || product.getSummary().length() > 140) {
			return flag;
		}
		// 内容长度[2,1000]
		if (product.getDetail().length() < 2 || product.getDetail().length() > 1000) {
			return flag;
		}

		productDao.addProduct(product);
		if (product.getId() != 0) {
			flag = true;
		}
		return flag;
	}

	public boolean updateProduct(Product product) {
		boolean flag = false;
		// 标题长度[2,80]
		if (product.getTitle().length() < 2 || product.getTitle().length() > 80) {
			return flag;
		}
		// 概要长度[2,140]
		if (product.getSummary().length() < 2 || product.getSummary().length() > 140) {
			return flag;
		}
		// 内容长度[2,1000]
		if (product.getDetail().length() < 2 || product.getDetail().length() > 1000) {
			return flag;
		}
		int lines = productDao.updateProduct(product);
		if (lines != 0) {
			flag = true;
		}
		return flag;
	}

	public boolean deleteProduct(int id) {
		boolean flag = false;
		int lines = productDao.deleteProduct(id);
		if (lines != 0) {
			flag = true;
		}
		return flag;
	}

	public List<Product> getProductsNoUser(User user) {
		return productDao.getProductsNoUser(user);
	}

}
